package com.xialj.demoend.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xialj.demoend.common.Result;
import com.xialj.demoend.entity.User;
import com.xialj.demoend.entity.UserExport;
import com.xialj.demoend.mapper.UserMapper;
import com.xialj.demoend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.IntStream;

/**
 * @Author xlj
 * @Date Created in  2023/2/24 13:25
 * @DESCRIPTION:  用户相关的操作
 * @Version V1.0
 */
@Service
public class UserServiceImpl implements UserService {
   private static final Snowflake SNOW_FLAKE = IdUtil.getSnowflake(1,1);
   @Autowired
   private UserMapper userMapper;

   @Override
   public Result allUserMessage(User user) {
      Page<User> userMessageByPage = PageHelper.startPage(user.getPage(), user.getLimit());
      List<User> userList = userMapper.allUserMessage(user);
      long total = userMessageByPage.getTotal();
      List<User> resultData = userMessageByPage.getResult();
      //只能这样放，不然会报错
      Result<List<User>> result = new Result<>();
      result.setTotal(total);
      result.setData(resultData);
      result.setMessage("true");
      result.setCode(200);
      return result;
   }

   @Override
   public int deleteUserMessageById(String id) {
      int result = userMapper.deleteUserMessageById(id);
      return result;
   }

   @Override
   public int insertOrUpdateUserMessage(User user) {
      int result = 0;
      if (user.getId() == null){
         //新增
         user.setId(IdUtil.simpleUUID());
         result = userMapper.insert(user);
      }else {
         //编辑
         result = userMapper.update(user);
      }
      return result;
   }

   @Override
   public int deleteUserMessageByIds(List<String> ids) {
      int result = userMapper.deleteUserMessageByIds(ids);
      return result;
   }

   @Override
   public List<UserExport> getAllUsers() {
      Optional<List<UserExport>> optionalUserList = Optional.of(userMapper.getAllUsers());
      if (optionalUserList.isPresent()) {
         List<UserExport> userList = optionalUserList.get();
         // 在这里进行对userList的操作，因为我们已经确定它不是null了
         userList.forEach(user -> {
            user.setId("0");
            if (user.getEmail() == null) {
               user.setEmail("--");
            }
            if (user.getPhone() == null) {
               user.setPhone("--");
            }
            if (user.getAddress() == null) {
               user.setAddress("--");
            }
         });
         IntStream.range(0, userList.size()).forEach(i -> {
            userList.get(i).setId(String.valueOf(i + 1)); // 将id赋值为1, 2, 3...
         });
         return userList;
      } else {
         // 如果userList是null，则执行此处的代码块
         return null;
      }
   }

   @Override
   public void saveAll(List<User> userList) {
         // 在这里进行对userList的操作，因为我们已经确定它不是null了
      userList.forEach(user -> {
            user.setId(IdUtil.simpleUUID());
            userMapper.insertUser(user);
         });
      }
}
